package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.net.API;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.utils.CheckIdCard;
import com.yxq.mdt.app.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 申请预备团长页面
 */
public class PreLeaderApplyActivity extends BaseActivity {

    @BindView(R.id.apply_et_name)
    EditText applyEtName;
    @BindView(R.id.apply_et_card)
    EditText applyEtCard;
    @BindView(R.id.apply_tv_area)
    EditText applyTvArea;
    @BindView(R.id.apply_tv_tel)
    EditText applyTvTel;
    @BindView(R.id.apply_btn_submit)
    Button applyBtnSubmit;
    @BindView(R.id.become_check_box)
    CheckBox becomeCheckBox;
    @BindView(R.id.become_check_agreement)
    TextView becomeCheckAgreement;

    @Override
    public int getLayoutId() {
        return R.layout.activity_pre_leader_apply;
    }

    @Override
    public void initView() {
        becomeCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                applyBtnSubmit.setBackgroundResource(R.drawable.shape_button_submit_red_selector);
                applyBtnSubmit.setTextColor(getResources().getColor(R.color.white));
            } else {
                applyBtnSubmit.setBackgroundResource(R.drawable.shape_button_submit_gray_selector);
                applyBtnSubmit.setTextColor(getResources().getColor(R.color.color_font_gray));
            }
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @OnClick({R.id.apply_btn_submit, R.id.become_check_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.apply_btn_submit:
                submit();
                break;
            case R.id.become_check_agreement:
                Intent intent = new Intent(mActivity, MiWebViewActivity.class);
                intent.putExtra("url", API.TEAM_WITHDRAWAL_PRE);
                startActivity(intent);
                break;
        }
    }

    /**
     * 发起预备团长代理
     */
    private void submit() {
        if (applyEtName.getText().toString().trim().length() == 0) {
            ToastUtils.showLong("请填写真实姓名");
            return;
        }
        if (applyEtCard.getText().toString().trim().length() == 0) {
            ToastUtils.showLong("请填写身份证号");
            return;
        }
        if (!CheckIdCard.check(applyEtCard.getText().toString().trim())) {
            ToastUtils.showLong("请填写合法的身份证号");
            return;
        }
        if (applyTvArea.getText().toString().trim().length() == 0) {
            ToastUtils.showLong("请填写您的职业");
            return;
        }
        if (applyTvTel.getText().toString().trim().length() == 0) {
            ToastUtils.showLong("请填写手机号");
            return;
        }
        if (!StringUtils.isMobileExact(applyTvTel.getText())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
        if (!becomeCheckBox.isChecked()) {
            ToastUtils.showShort("请先同意《预备团长协议》");
            return;
        }
        showLoadingDialog();
        AuctionModel.applyPrepared(applyEtName.getText().toString(), applyEtCard.getText().toString(), applyTvArea.getText().toString(), applyTvTel.getText().toString(), new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                hideLoadingDialog();
                JSONObject jsonObject = JSON.parseObject(s);
                int status = jsonObject.getIntValue("state");
                if (status == 1) {
                    ToastUtils.showLong("申请成功,审核成功将会收到短信通知。");
                    finish();
                } else if (status == 0) {
                    ToastUtils.showLong("申请中,审核成功将会收到短信通知。");
                    finish();
                } else {
                    ToastUtils.showLong("申请失败");
                }

            }

            @Override
            public void onError(String errMsg) {
                hideLoadingDialog();
                ToastUtils.showLong(errMsg);
            }
        });
    }
}
